package com.android.pagemarket.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Toast;

import com.android.pagemarket.MyApplication;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-8-8
 * @see : 通用不好分类的工具类
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class Util
{   
    public static void showToast(int msg)
    {
        Toast.makeText(MyApplication.getContext(), msg, Toast.LENGTH_LONG).show(); 
    }
    
    public static void showToast(String msg)
    {
        Toast.makeText(MyApplication.getContext(), msg, Toast.LENGTH_LONG).show(); 
    }
    
    public static boolean isEmpty(String str)
    {
      return ((str == null) || (str.length() == 0) || (str.equals("null")));
    }
    
    public static String getExternDir(String dir)
    {
        String path = Environment.getExternalStorageDirectory().getAbsolutePath();
        path += dir;
        return path;
    }

    public static boolean isConnectionFast(int type, int subType)
    {
        if (type == ConnectivityManager.TYPE_WIFI)
        {
            return true;
        } else if (type == ConnectivityManager.TYPE_MOBILE)
        {
            switch (subType)
            {
            case TelephonyManager.NETWORK_TYPE_1xRTT:
                return false; // ~ 50-100 kbps
            case TelephonyManager.NETWORK_TYPE_CDMA:
                return false; // ~ 14-64 kbps
            case TelephonyManager.NETWORK_TYPE_EDGE:
                return false; // ~ 50-100 kbps
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
                return true;  // ~ 400-1000 kbps
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
                return true;  // ~ 600-1400 kbps
            case TelephonyManager.NETWORK_TYPE_GPRS:
                return false; // ~ 100 kbps
            case TelephonyManager.NETWORK_TYPE_HSDPA:
                 return true; // ~ 2-14 Mbps
            case TelephonyManager.NETWORK_TYPE_HSPA:
                 return true; // ~ 700-1700 kbps
            case TelephonyManager.NETWORK_TYPE_HSUPA:
                 return true; // ~ 1-23 Mbps
            case TelephonyManager.NETWORK_TYPE_UMTS:
                return true;  // ~ 400-7000 kbps
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return false; // ~25 kbps
            case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                return false;
            default:
                return false;
            }
        } else
        {
            return false;
        }
    }
    
    // 判断是否是 3G 或者 wifi 网络
    public boolean isWifi3GNetwork(Context context)
    {
        ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);  
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        TelephonyManager tele = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        if (activeNetInfo != null)
        { 
            if(activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) 
            {  
                return true;  
            }else
            if(activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE)
            {
                return Util.isConnectionFast(activeNetInfo.getType(), tele.getNetworkType());
            }else 
            {
                return false;
            }
        }else
        {
            return false;
        }
    }
    
    public static int[] getLocationOnScreenAtB(View achor, Activity context)
    {
        int[] cordi = new int[2];
        if(achor != null)
        {
            achor.getLocationOnScreen(cordi);
        }
        DisplayMetrics dm=new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        cordi[1] = dm.heightPixels - cordi[1];
        
        return cordi;
    }
    
    public static int[] getLocationOnScreenAtT(View achor, Activity context)
    {
        int[] cordi = new int[2];
        if(achor != null)
        {
            achor.getLocationOnScreen(cordi);
        }
        cordi[1] += achor.getHeight();
        cordi[0] -= achor.getWidth();
        return cordi;
    }
    
    public static boolean isChinese(char c)
    {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) 
        { 
            return true;
        }
        return false;
    }
    
    public static boolean isChinese(String str)
    {
        if(TextUtils.isEmpty(str)) return false;
        int len = str.length();
        for(int i =0; i < len; i++)
        {
            if(!isChinese(str.charAt(i)))
            {
                return false;
            }
        }
        return true;
    }

    /** 是否是电话号码 */
    public static boolean isPhoneNumberValid(String phoneNumber) 
    {
        // (^(\d{2,4}[-_－—]?)?\d{3,8}([-_－—]?\d{3,8})?([-_－—]?\d{1,7})?$)|(^0?1[35]\d{9}$)
        String expression1 = "((^(13|15|18)[0-9]{9}$)|(^0[1,2]{1}\\d{1}-?\\d{8}$)|(^0[3-9] {1}\\d{2}-?\\d{7,8}$)|(^0[1,2]{1}\\d{1}-?\\d{8}-(\\d{1,4})$)|(^0[3-9]{1}\\d{2}-? \\d{7,8}-(\\d{1,4})$))";
        String expression2 = "((\\d{11})|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)";
        CharSequence inputStr = phoneNumber;
        Pattern pattern1 = Pattern.compile(expression1);
        Matcher matcher1 = pattern1.matcher(inputStr);
        Pattern pattern2 = Pattern.compile(expression2);
        Matcher matcher2 = pattern2.matcher(inputStr);
        return matcher1.matches() || matcher2.matches();
    }
    
    public static boolean isValidName(String name)
    {
        String expression1 = "[（）()A-Za-z0-9\u4e00-\u9fa5]*";
        Pattern pattern1 = Pattern.compile(expression1);
        Matcher matcher1 = pattern1.matcher(name);
        
        String expression2 = "[0-9]*";
        Pattern pattern2 = Pattern.compile(expression2);
        Matcher matcher2 = pattern2.matcher(name);
        
        return matcher1.matches() && !matcher2.matches();
    }
}
